package com.bw.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bw.home.R
import com.bw.home.model.HomeListEntity

class HomeListAdapter(private val dataList: HomeListEntity)
    :RecyclerView.Adapter<HomeListAdapter.MyViewHolder>() {
    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val titleView: TextView = itemView.findViewById(R.id.title)
        val contentView: TextView = itemView.findViewById(R.id.content)
        val contentTime: TextView = itemView.findViewById(R.id.content_time)
    }
    // 创建ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.home_list_item, parent, false)
        return MyViewHolder(view)
    }

    // 绑定数据
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val item = dataList[position]
        holder.titleView.text = item.title
        holder.contentView.text = item.content
        holder.contentTime.text = item.publishTime
    }

    // 获取数据数量
    override fun getItemCount() = dataList.size
}